<?php
// Webservice Elgg
// Returns the user's course list

require '../../claroline/inc/claro_init_global.inc.php';
require_once 'lib/UDADOC.lib.php';
require_once get_path('incRepositorySys')  . '/lib/fileManage.lib.php';
require_once get_path('incRepositorySys')  . '/lib/file.lib.php';

$baseServDir = get_path('coursesRepositorySys');
$baseServUrl = get_path('url') . '/';


if ($_GET['action'] == "courseList")
{
	include_once get_path('incRepositorySys') . '/lib/user.lib.php';
	
	
	$tbl_mdb_names       = claro_sql_get_main_tbl();
	    $tbl_course          = $tbl_mdb_names['course'];
	    $tbl_rel_course_user = $tbl_mdb_names['rel_course_user' ];
	    $tbl_user        = $tbl_mdb_names['user'];
	
	// Search for the user_id
	$sql = "SELECT user_id 
				 FROM `".$tbl_user."`
				 WHERE username='".$_GET['username']."'
				 ";
	$userId = claro_sql_query_get_single_value($sql);
	
	
	if ($userId)
	{
		$sql = "SELECT `C`.`code`       `code`,
		               `C`.`intitule`          `name`,
		               `C`.`faculte` 			 `category`
		        FROM `" . $tbl_course . "` AS C,
		             `" . $tbl_rel_course_user . "` AS CU
		        WHERE CU.`code_cours` = C.`code`
		          AND `CU`.`isCourseManager` = 1
		          AND CU.`user_id` = " . (int) $userId."
		          ORDER BY category, name";
		
		$courseList = claro_sql_query_fetch_all($sql);
		
		if ($courseList)
		{
			foreach ($courseList as $cours)
			{
				$output[$cours['category']][] = array("code"=>$cours['code'],"name"=>$cours['name']);
			}
		}
		else
		$output['error'][] = get_lang('No course to display');
	
		
	}
	else
	$output['error'][] = get_lang('Unknown user');
	
	$output = json_encode($output);
	print $output;
}

if ($_GET['action'] == 'createLink')
{
	// Create link in the right course
	print "connected";
}

// Remove links
if ($_GET['action'] == 'rmLink')
{
	// Get course context
	$_cid = $_GET['code'];
	$_SESSION['_cid'] = $_cid;
	$_course = claro_get_course_data($_cid,true);
	$_SESSION['_course'] = $_course;
	
	$courseDir   = claro_get_course_path().'/document';
	
	$baseWorkDir = $baseServDir.$courseDir;
	
	
	
	$tbl_cdb_names = claro_sql_get_course_tbl();

  $tbl_lp_learnPath            = $tbl_cdb_names['lp_learnPath'           ];
  $tbl_lp_rel_learnPath_module = $tbl_cdb_names['lp_rel_learnPath_module'];
  $tbl_lp_user_module_progress = $tbl_cdb_names['lp_user_module_progress'];
  $tbl_lp_module               = $tbl_cdb_names['lp_module'              ];
  $tbl_lp_asset                = $tbl_cdb_names['lp_asset'               ];

  $TABLELEARNPATH            = $tbl_lp_learnPath;
  $TABLELEARNPATHMODULE      = $tbl_lp_rel_learnPath_module;
  $TABLEUSERMODULEPROGRESS   = $tbl_lp_user_module_progress;
  $TABLEMODULE               = $tbl_lp_module;
  $TABLEASSET                = $tbl_lp_asset;

  $dbTable = $tbl_cdb_names['document'];
	
	
	
	// Look for Elgg link : [elggGuid:X]
	$sql = "SELECT path
					FROM `".$dbTable."`
					WHERE comment LIKE '[elggGuid:".$_GET['guid']."]%'";
	$documents = claro_sql_query_fetch_all_cols($sql);
	
	if ($documents)
	{
			$deletedFiles = array();
			
			foreach ($documents['path'] as $fileToDelete)
			{
				if (UDADOCremoveItem($fileToDelete,$baseWorkDir,true))
				$deletedFiles[] = $fileToDelete;
			}
	
			if (count($deletedFiles) != count($documents['path']))
			{
				echo get_lang("Some files could not be deleted").".\n";
			}
	
			exit;
	}
}
?>